package com.payment.ktb.activity.main1;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.activity.MainActivity;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.ConstantsPay;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.Md5Utils;
import com.payment.ktb.utils.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/6/12.
 */
public class NocardOrderResultWaitActivity extends BaseActivity {

    int count = 0;


    @BindView(R.id.tv_nocardorderresultwait_amount)
    TextView tv_nocardorderresultwait_amount;
    @BindView(R.id.tv_nocardorderresultwait_time)
    TextView tv_nocardorderresultwait_time;
    @BindView(R.id.tv_nocardorderresultwait_orderno)
    TextView tv_nocardorderresultwait_orderno;




    @Override
    protected void onPause() {
        super.onPause();
        count = 0;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_nocardorderresultwait);
        ButterKnife.bind(this);
        hideTitleBack();
        getOrderStatus();
        initTitle("支付结果");
    }


    /**
     *
     */
    private void initData() {
        tv_nocardorderresultwait_amount.setText("¥ " + handleFen2Yuan(getIntent().getStringExtra("amount")));
        tv_nocardorderresultwait_time.setText(getIntent().getStringExtra("orderTime"));
        tv_nocardorderresultwait_orderno.setText(getIntent().getStringExtra("orderNo"));
    }

    private String handleFen2Yuan(String fen) {
        if (fen.equals("0")) {
            return "0.00";
        }
        double srcFen = Double.valueOf(fen);
        double roundSrcFen = Math.round(srcFen);
        return String.valueOf(roundSrcFen/100);
    }


    @OnClick({R.id.btn_nocardorderresultwait_back,R.id.btn_nocardorderresultwait_refresh})
    public void clickEvent(View view) {

        switch (view.getId()) {



            //返回商户
            case R.id.btn_nocardorderresultwait_back:
                startActivity(MainActivity.class);
                break;

            case R.id.btn_nocardorderresultwait_refresh:
                getOrderStatus();
                break;

        }



    }



    //查询订单状态
    public void getOrderStatus() {


        progressDialogUtils.showProgress("正在查询订单订单状态...");

        //构造传输参数
        final Map<String, String> map = new HashMap<String,String>();
        map.put("linkId",new Date().getTime() + "");
        map.put("orderNo",getIntent().getStringExtra("orderNo"));

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.PayOrderStatus, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);

                    Intent intent = null;

                    if (jsonObject.optString("orderStatus").equals("20")) {
                        intent = new Intent(mContext,OrderConfirmSuccessActivity.class);
                        intent.putExtra("amount",getIntent().getStringExtra("amount"));
                        intent.putExtra("goodsName",jsonObject.optString("goodsName"));
                        intent.putExtra("cardBank",getIntent().getStringExtra("cardBank"));
                        intent.putExtra("cardNo",getIntent().getStringExtra("cardNo"));
                        intent.putExtra("orderNo",jsonObject.optString("orderNo"));
                        intent.putExtra("orderTime",jsonObject.optString("orderTime"));
                        intent.putExtra("ordermemo",jsonObject.optString("ordermemo"));
                        startActivity(intent);
                    }else if (jsonObject.optString("orderStatus").equals("21")){
                        intent = new Intent(mContext,OrderConfirmFailActivity.class);
                        intent.putExtra("amount",getIntent().getStringExtra("amount"));
                        intent.putExtra("goodsName",jsonObject.optString("goodsName"));
                        intent.putExtra("cardBank",getIntent().getStringExtra("cardBank"));
                        intent.putExtra("cardNo",getIntent().getStringExtra("cardNo"));
                        intent.putExtra("orderNo",jsonObject.optString("orderNo"));
                        intent.putExtra("orderTime",jsonObject.optString("orderTime"));
                        intent.putExtra("ordermemo",jsonObject.optString("ordermemo"));
                        startActivity(intent);
                    }else {

                        if (count < 3) {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    getOrderStatus();
                                }
                            }, 4000);
                        } else {
                            initData();
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }







}
